/**
 * 版权所有:版权所有(C) 2020，卓信科技
 * 系统名称:FOL2020-财务在线
 * 文件名称:SysDictController.java
 * 模块名称:SysDictController视图层
 * 创建日期:2020年12月07日 下午04:36:07
 * 作        者:wangyihan
 * 内容摘要:字典表
 */
package com.zx.emanage.sysmanage.web;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.zx.emanage.sysmanage.service.ISysDictService;
import com.zx.emanage.util.gen.entity.SysDict;


/**
 * 类  名  称:SysDictController
 * 内容摘要:字典表
 * 作        者:wangyihan
 */
@Controller
@RequestMapping("/sys")
public class SysDictController
{
    @Autowired
    private ISysDictService sysDictService;

    @RequestMapping(value = "/getDictAllList.do", method = { RequestMethod.GET, RequestMethod.POST }, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getDictAllList(final HttpServletRequest request)
    {
        // JSON转换器
        Gson gson = new Gson();
        Map<String, Object> parentMap = new LinkedHashMap<String, Object>();
        final String key_code = request.getParameter("key_code");
        final String value_meaning = request.getParameter("value_meaning");
        Map<String, Object> search = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(key_code))
        {
            search.put("key_code", key_code);
        }
        if (StringUtils.isNotEmpty(value_meaning))
        {
            search.put("value_meaning", value_meaning);
        }
        List<SysDict> parentNodes = sysDictService.getDictAll(search);
        // 如果查询时候输入父节点 子节点也应该查出来
        if (StringUtils.isNotEmpty(key_code) || StringUtils.isNotEmpty(value_meaning))
        {
            for (int j = 0; j < parentNodes.size(); j++)
            {
                if ("0".equals(parentNodes.get(j).getP_sys_dict_id()))
                {
                    List<SysDict> parentNodess = sysDictService.getByParentDictId(parentNodes.get(j).getSys_dict_id().toString());
                    parentNodes.addAll(parentNodess);
                }
            }
        }
        parentMap.put("total", sysDictService.getDictCount(search));
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < parentNodes.size(); i++)
        {
            Integer in = Integer.valueOf(parentNodes.get(i).get_parentId());
            if (in == 0)
            {
                parentNodes.get(i).set_parentId(null);
            }

        }

        List<SysDict> mapFortree = makeTree(parentNodes);

        parentMap.put("rows", mapFortree);
        String gjson = gson.toJson(parentMap);
        return gjson;
    }

    static List<SysDict> makeTree(List<SysDict> parentNodes)
    {
        List<SysDict> newList = new ArrayList<SysDict>();

        for (int i = 0; i < parentNodes.size(); i++)
        {
            if (parentNodes.get(i).getSort_order() == null)
            {
                parentNodes.get(i).setSort_order("0");
            }
            if (i == 0 || parentNodes.get(i).getDict_level() == parentNodes.get(0).getDict_level())
            {
                newList.add(parentNodes.get(i));
            }
            else
            {
                for (int j = 0; j < newList.size(); j++)
                {
                    if (newList.get(j).getSort_order() == null)
                    {
                        newList.get(j).setSort_order("0");
                    }
                    if (parentNodes.get(i).getP_sys_dict_id().equals(newList.get(j).getSys_dict_id().toString()))
                    {
                        if (j + 1 == newList.size() || parentNodes.get(i).getDict_level() != newList.get(j + 1).getDict_level())
                        {
                            newList.add(j + 1, parentNodes.get(i));
                            break;
                        }
                    }
                    else
                    {
                        if (parentNodes.get(i).getP_sys_dict_id().equals(newList.get(j).getP_sys_dict_id()))
                        {
                            if (Integer.parseInt(parentNodes.get(i).getSort_order()) <= Integer.parseInt(newList.get(j).getSort_order()))
                            {
                                newList.add(j, parentNodes.get(i));
                                break;
                            }
                            else
                            {
                                if (j + 1 == newList.size() || !parentNodes.get(i).getP_sys_dict_id().equals(newList.get(j + 1).getP_sys_dict_id()) || Integer.parseInt(parentNodes.get(i).getSort_order()) <= Integer.parseInt(newList.get(j + 1).getSort_order()))
                                {
                                    newList.add(j + 1, parentNodes.get(i));
                                    break;
                                }
                            }

                        }
                    }
                }
            }
        }

        return newList;
    }


    /**
     * 业务数据管理 :保存业务数据
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveDictData.do", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    @Transactional
    public String saveDictData(HttpServletRequest request, HttpServletResponse response)
    {
        // value_meaning key_code sort_order remark
         String value_meaning = request.getParameter("value_meaning");
         String key_code = request.getParameter("key_code");
         String sort_order = request.getParameter("sort_order");
         String remark = request.getParameter("remark");
         String p_sys_dict_id = request.getParameter("parent_id");
         String dict_level = request.getParameter("level");
         
         SysDict sysdict=new SysDict();
         sysdict.setValue_meaning(value_meaning);
         sysdict.setKey_code(key_code);
         sysdict.setSort_order(sort_order);
         sysdict.setRemark(remark);
         sysdict.setDict_level(Integer.parseInt(dict_level));
         sysdict.setP_sys_dict_id(p_sys_dict_id);
        sysdict.setCreate_timestamp(new Timestamp(System.currentTimeMillis()));
        return sysDictService.saveDictData(sysdict);
    }
    @RequestMapping(value = "/getByDictId.do", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Map<String,Object> getByDictId(HttpServletRequest request, HttpServletResponse response)
    {
        String sys_dict_id = request.getParameter("sys_dict_id");
        return sysDictService.getByDictId(sys_dict_id);
    }
    /**
     * 业务数据维护:更新业务数据
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/updateDictData.do", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    @Transactional
    public String updateDictData(HttpServletRequest request, HttpServletResponse response)
    {
        String value_meaning = request.getParameter("value_meaning");
        String key_code = request.getParameter("key_code");
        String sort_order = request.getParameter("sort_order");
        String remark = request.getParameter("remark");
        String sys_dict_id = request.getParameter("sys_dict_id");
        String dict_level = request.getParameter("level");
        String p_sys_dict_id = request.getParameter("parent_id");
        SysDict sysdict = new SysDict();
        sysdict.setValue_meaning(value_meaning);
        sysdict.setKey_code(key_code);
        sysdict.setSort_order(sort_order);
        sysdict.setRemark(remark);
        sysdict.setSys_dict_id(Integer.parseInt(sys_dict_id));
        sysdict.setDict_level(Integer.parseInt(dict_level));
        sysdict.setP_sys_dict_id(p_sys_dict_id);
        return sysDictService.updateDictData(sysdict);

    }


    /**
     * 业务数据维护 :删除业务数据
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delDictData.do", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    @Transactional
    public String delData(HttpServletRequest request, HttpServletResponse response)
    {
        String idList = request.getParameter("idList");
        return sysDictService.delDictData(idList);
    }

    /**
     * 
     * @Title: selectAllDicts
     * @Description: TODO(获取整个字典表的key  value)
     * @param request
     * @param response
     * @return 
     * @author: Quwenrui
     * @time:2018年3月22日 上午10:54:58
     * history:
     * 1、2018年3月22日 Quwenrui 创建方法
     */
    @RequestMapping(value = "/selectAllDicts.do", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Map<String, Object> selectAllDicts(HttpServletRequest request, HttpServletResponse response)
    {
        return sysDictService.selectAllDicts();
    }


    /**
     * 
     * @Title: getDictListByKey_code
     * @Description: TODO(通过Key_code获取其子集)
     * @param request
     * @return 
     * @author: Quwenrui
     * @time:2018年6月5日 上午10:13:52
     * history:
     * 1、2018年6月5日 Quwenrui 创建方法
     */
    @RequestMapping(value = "/getDictListByKey_code.do", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public List<Map<String, Object>> getDictListByKey_code(HttpServletRequest request)
    {
        String key_code = request.getParameter("key_code");
        return sysDictService.getDictListByKey_code(key_code);
    }

    /**
     * 
     * @Title: getSelectsList
     * @Description: TODO(从字典表获取select中的option)
     * @param request
     * @return 返回Map   key_code为key,一堆option为value
     * @author: Quwenrui
     * @time:2018年4月20日 下午4:31:44
     * history:
     * 1、2018年4月20日 Quwenrui 创建方法
     */
    @RequestMapping(value = "/getSelectsList.do", method = { RequestMethod.POST })
    @ResponseBody
    public Map<String, Object> getSelectsList(HttpServletRequest request)
    {

        return sysDictService.getSelectsList();
    }
}
